package yl.hs.bmipfull.service.system.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.system.IAppSettingDao;
import yl.hs.bmipfull.pojo.system.AppSettingEntity;
import yl.hs.bmipfull.service.system.IAppSettingService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.SubmitModel;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AppSettingServiceImpl
 *
 * @author 谢景广
 * @category
 * @date 2021-05-13
 */
@Service
public class AppSettingServiceImpl extends SystemBaseService implements IAppSettingService {

    @Autowired
    IAppSettingDao appSetDao;

    @Override
    public List<AppSettingEntity> queryAllUnitSn(String[] unitSN) {
        var datalist = appSetDao.queryAll(unitSN);
        datalist.sort(Comparator.comparing(AppSettingEntity::getId));
        return datalist;
    }

    @Transactional
    public Integer saveCreateAppSet(SubmitModel model) {
        var appSet = new AppSettingEntity();
        appSet.setOrganizationSN(model.getSn());
        appSet.setParamCode(model.getData().get("paramCode").toString());
        appSet.setParamName(model.getData().get("paramName").toString());
        if (!checkSetParamCodeExists(appSet.getParamCode(), model.getSn())) {
            return appSetDao.insertNew(appSet);
        }
        var allSets = appSetDao.queryAll(new String[]{model.getSn()});
        HsApp.setSystemSet(allSets);
        return 0;
    }

    @Transactional
    public Integer saveUpdateAppSets(Map<String, Object> model) {
        var list = appSetDao.queryAll(model.get("sn").toString().split(","));
        var data = (List<Map<String, Object>>) model.get("data");
        var count = 0;

        for (var setData : data) {
            var appSet = list.stream().filter(t -> t.getParamCode().equals(setData.get("paramCode").toString())).findFirst().get();
            var oldValue = appSet.getParamValue();
            String newValue = "";
            if (setData.get("paramValue") != null) {
                newValue = setData.get("paramValue").toString();
            }
            if (newValue != null && newValue.equals(oldValue) == false) {
                appSet.setParamValue(newValue);
                count += appSetDao.update(appSet);
                try {
                    writeLog(ActionType.UPDATE, appSet.getParamCode(),
                            String.format("修改%s:%s -> %s", appSet.getParamName(), oldValue, newValue),
                            appSet.toJson(), appSet.getClass());
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        }
        var st = list.stream().map(t->t.getOrganizationSN()).distinct().toList();
        var allSets = appSetDao.queryAll(st.toArray(new String[st.size()]));
        HsApp.setSystemSet(allSets);
        return count;
    }

    @Override
    public Boolean checkSetParamCodeExists(String code, String unitSN) {
        Integer result = appSetDao.checkSetParamCodeExists(code, unitSN);
        return result > 0;
    }

    @Override
    public Map<String, Object> querySetObjByUnitSn(String[] unitSN) {
        var allSets = appSetDao.queryAll(unitSN);
        HsApp.setSystemSet(allSets);
        var map = new HashMap<String, Object>();
        if (allSets != null && allSets.size() > 0) {
            allSets.forEach(t -> {
                if (!map.containsKey(t.getParamCode())) {
                    map.put(t.getParamCode(), t.getParamValue());
                }
            });
        }
        return map;
    }

    @Override
    public AppSettingEntity findByCode(String code, String unitSN){
       return appSetDao.getByCodeAndUnitSN(code,unitSN);
    }

    @Override
    public AppSettingEntity getByParamCode(String code) {
        return appSetDao.getByParamCode(code);
    }

    @Override
    public int save(AppSettingEntity stVers){
        var result = 0;
        if(stVers.getId()==null){
            result = appSetDao.insertNew(stVers);
        }
        else {
            result = appSetDao.update(stVers);
        }
        var allSets = appSetDao.queryAll(new String[]{stVers.getOrganizationSN()});
        HsApp.setSystemSet(allSets);
        return result;
    }
}
